package com.sinooceangroup.voiceassistant;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sinooceanland.commonutils.SharedPreferencesUtil;

import static com.sinooceangroup.voiceassistant.Constants.FEMALE_VOICE;
import static com.sinooceangroup.voiceassistant.Constants.MALE_VOICE;
import static com.sinooceangroup.voiceassistant.Constants.SWITCH_VOICE;

/**
 * 语音助手设置界面
 */
public class SettingsActivity extends Activity {

    private CheckBox cb_voice;
    private RadioGroup rg_speakingRate, rg_maleVoiceFemaleVoice;
    private LinearLayout iv_back;
    private RadioButton maleVoice, femaleVoice, times1, times1_5, times2;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        initView();
        onChecked();
    }

    private void initView() {
        cb_voice = (CheckBox) findViewById(R.id.cb_voice);
        rg_speakingRate = (RadioGroup) findViewById(R.id.rg_speakingRate);
        times1 = (RadioButton) findViewById(R.id.times1);
        times1_5 = (RadioButton) findViewById(R.id.times1_5);
        times2 = (RadioButton) findViewById(R.id.times2);
        maleVoice = (RadioButton) findViewById(R.id.maleVoice);
        femaleVoice = (RadioButton) findViewById(R.id.femaleVoice);
        rg_maleVoiceFemaleVoice = (RadioGroup) findViewById(R.id.rg_maleVoiceFemaleVoice);
        iv_back = (LinearLayout) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setData();
    }

    private void setAllRadioButtonEnable(boolean enable) {
        findViewById(R.id.times1).setEnabled(enable);
        findViewById(R.id.times1_5).setEnabled(enable);
        findViewById(R.id.times2).setEnabled(enable);
        findViewById(R.id.maleVoice).setEnabled(enable);
        findViewById(R.id.femaleVoice).setEnabled(enable);
    }

    private void setAllRadioButtonChecked() {
        rg_maleVoiceFemaleVoice.clearCheck();
        rg_speakingRate.clearCheck();
    }

    private void setData() {
        cb_voice.setChecked(SharedPreferencesUtil.getBoolean(this, SWITCH_VOICE, true));
        if (SharedPreferencesUtil.getBoolean(this, SWITCH_VOICE, true)) {
            switch (SharedPreferencesUtil.getString(this, Constants.SPEAKING_RATE, "50")) {
                case "50":
                    times1.setChecked(true);
                    break;
                case "75":
                    times1_5.setChecked(true);
                    break;
                case "100":
                    times2.setChecked(true);
                    break;
            }
            switch (SharedPreferencesUtil.getString(this, Constants.MALE_OR_FEMALE_VOICE, FEMALE_VOICE)) {
                case MALE_VOICE:
                    maleVoice.setChecked(true);
                    break;
                case FEMALE_VOICE:
                    femaleVoice.setChecked(true);
                    break;
            }
        }
    }

    private void onChecked() {
        cb_voice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferencesUtil.putBoolean(SettingsActivity.this, SWITCH_VOICE, isChecked);
                setAllRadioButtonEnable(isChecked);
                if (isChecked) {
                    setData();
                } else {
                    setAllRadioButtonChecked();
                }
            }
        });
        rg_speakingRate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.times1) {
                    SharedPreferencesUtil.putString(SettingsActivity.this, Constants.SPEAKING_RATE, "50");
                } else if (checkedId == R.id.times1_5) {
                    SharedPreferencesUtil.putString(SettingsActivity.this, Constants.SPEAKING_RATE, "75");
                } else if (checkedId == R.id.times2) {
                    SharedPreferencesUtil.putString(SettingsActivity.this, Constants.SPEAKING_RATE, "100");
                }
            }
        });
        rg_maleVoiceFemaleVoice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.maleVoice) {
                    SharedPreferencesUtil.putString(SettingsActivity.this, Constants.MALE_OR_FEMALE_VOICE, Constants.MALE_VOICE);
                } else if (checkedId == R.id.femaleVoice) {
                    SharedPreferencesUtil.putString(SettingsActivity.this, Constants.MALE_OR_FEMALE_VOICE, Constants.FEMALE_VOICE);
                }
            }
        });
    }

}
